<?php 
namespace Modules\AuthAdmin\Http\Controllers\v1;
use Illuminate\Http\Request;
use Modules\AuthAdmin\Http\Requests\CommonIdRequest;
use Modules\AuthAdmin\Http\Requests\CommonStatusRequest;
use Modules\AuthAdmin\Http\Requests\AdminCreateRequest;
use Modules\AuthAdmin\Http\Requests\AdminUpdateRequest;
use Modules\AuthAdmin\Models\AuthAdmin as AuthAdminModel;
use Modules\AuthAdmin\Models\AuthGroup as AuthGroupModel;
class AuthAdminController extends CommonController
{
	/**
	 * @name  管理员列表
	 * @method  GET	
	 * @param  page int 页码
	 * @param  pagesize int 每页条数
	 * @param  name string 账号	
	 * @param  group_id int 权限组ID
	 * @param  status int 状态:0=禁用,1=启用
	 * @param  created_at array 创建时间	
	 * @param  updated_at array 更新时间	
	 * @return  JSON
	 */
    public function index(Request $request)
    {
		return (new AuthAdminModel())->list_data($request->all());
    }
	/**
	 * @name  获取权限组
	 * @method  GET	
	 * @return  JSON
	 */
    public function group_list()
    {	
		$AuthGroupModel = new AuthGroupModel();
		return $AuthGroupModel->api_success([
			'data'=>$AuthGroupModel->orderBy('id','desc')->select('id','name')->get()->toArray()
		]);
    }
	/**
	 * @name  添加
	 * @method  POST	
	 * @param  name string 名称	
	 * @param  phone string 手机号	
	 * @param  username string 账号	
	 * @param  password string 密码	
	 * @param  password_confirmation string 确认密码	
	 * @param  group_id int 权限组ID	
	 * @return  JSON
	 */
    public function store(AdminCreateRequest $request)
    {
		$data = $request->all();
		return (new AuthAdminModel())->admin_create([
				'name'=>$data['name'],
				'phone'=>$data['phone'],
				'username'=>$data['username'],
				'password'=>$data['password'],
				'group_id'=>$data['group_id'],
		]);
    }
	/**
	 * @name  编辑页面
	 * @method  GET	
	 * @param  id int 管理员id	
	 * @return  JSON
	 */
    public function edit(CommonIdRequest $request)
    {	
		$AuthGroupModel = new AuthGroupModel();
		return $AuthGroupModel->api_success([
			'data'=>(new AuthAdminModel())->select('id','name','group_id','phone','username')->find($request->get('id'))
		]);
    }
	/**
	 * @name  编辑提交
	 * @method  PUT
	 * @param  id int 管理员id		 
	 * @param  name string 名称	
	 * @param  phone string 手机号	
	 * @param  username string 账号	
	 * @param  password string 密码	
	 * @param  password_confirmation string 确认密码	
	 * @param  group_id int 权限组ID	
	 * @return  JSON
	 */ 
    public function update(AdminUpdateRequest $request)
    {
		$data = $request->all();
		return (new AuthAdminModel())->admin_update($data['id'],[
				'name'=>$data['name'],
				'group_id'=>$data['group_id'],
				'phone'=>$data['phone'],
				'username'=>$data['username']
		]); 
    }
	/**
	 * @name  调整状态
	 * @method  PUT	
	 * @param  id int 管理员id	
	 * @param  status int 状态（0或1）
	 * @return  JSON
	 */ 
    public function status(CommonStatusRequest $request)
    {
		$data = $request->all();
		return (new AuthAdminModel())->admin_status_update($data['id'],['status'=>$data['status']]); 
    }
	/**
	 * @name  初始化密码
	 * @method  PUT	
	 * @param  id int 管理员id	
	 * @return  JSON
	 */ 
    public function update_pwd(CommonIdRequest $request)
    {
		$data = $request->all();
		return (new AuthAdminModel())->admin_status_update($data['id'],['password'=>bcrypt('123456')],'初始化成功！','初始化失败！'); 
    }
}
